<template>
  <div class="container">
    <!-- <div class="mask"
         v-if="!islogin">
      点击立即登录
      <button open-type="getUserInfo"
              @getuserinfo="get_info"
              class="btn"></button>
    </div> -->
    <web-view v-if="islogin"
              :src="h5_url"></web-view>
  </div>

</template>
<script>
import common from '@/assets/js/mmk_common.js'
export default {
  data () {
    return {
      h5_url: "",
      islogin: false
    };
  },
  methods: {
    get_info (e) {
      let that = this;
      wx.getSetting({
        success: res => {
          if (res.authSetting["scope.userInfo"] === true) {
            // 已经授权，不会弹框
            // 成功授权
            if (wx.getStorageSync("user_token")) {
              // 登陆了
            } else {
              // 没登陆
              wx.showLoading({
                title: "Loading...", //提示的内容,
                mask: true, //显示透明蒙层，防止触摸穿透,
                success: res => { }
              });
              wx.login({
                success: result => {
                  common.fly_post("api/v4_2/minapp/login", {
                    code: result.code,
                    iv: e.mp.detail.iv,
                    encryptedData: e.mp.detail.encryptedData,
                    avatarUrl: e.mp.detail.userInfo.avatarUrl,
                    nickName: e.mp.detail.userInfo.nickName
                  }, response => {
                    wx.hideLoading();
                    let res = response.data;
                    if (res.status_code == 0) {
                      wx.setStorageSync("user_token", res.data.token);
                      wx.setStorageSync("user_id", res.data.user_id);
                      wx.setStorageSync("open_id", res.data.user_jwt);
                      console.log(12355,res.data.user.data.role_id)
                wx.setStorageSync("role_id", res.data.user.data.role_id);
                console.log(444444,wx.getStorageSync("role_id"))
                      wx.redirectTo({ url: '/pages/package_mine/sign_in/main' });
                    } else {
                      wx.hideLoading();
                      wx.showToast({
                        title: res.message,
                        icon: "none",
                        duration: 2000,
                        mask: false
                      });
                    }
                  })
                },
                fail: () => {
                  wx.showToast({
                    title: "登录失败", //提示的内容,
                    icon: "none", //图标,
                    duration: 1500, //延迟时间,
                    mask: true, //显示透明蒙层，防止触摸穿透,
                    success: res => { }
                  });
                }
              });
            }
          } else if (res.authSetting["scope.userInfo"] === false) {
            // 授权弹窗被拒绝
            wx.showToast({
              title: "授权被禁用，请前往设置", //提示的内容,
              icon: "none", //图标,
              duration: 2000, //延迟时间,
              mask: false, //显示透明蒙层，防止触摸穿透,
              success: res => {
                wx.openSetting({
                  success: res => { },
                  fail: res => { }
                });
              }
            });
          } else {
            // 没有弹出过授权弹窗
            wx.authorize({
              scope: "scope.userInfo",
              success: res => {
                wx.redirectTo({ url: '/pages/package_mine/sign_in/main' });
              },
              fail: () => { },
              complete: () => { }
            });
          }
        }
      });
    }
  },
  onLoad () {
    let token = wx.getStorageSync("user_token");
    let province_info = wx.getStorageSync("province_info") ? JSON.parse(wx.getStorageSync("province_info")) : {};
    if (token) {
      // 已登录
      this.islogin = true;
      this.h5_url = `${common.sign_in_url}?user_token=${token}&province_id=${province_info.province_id || 0}`;
      console.log(this.h5_url);

    } else {
      // 未登录
      wx.navigateTo({ url: '/pages/package_main/login/main' });
    }
    //  this.h5_url = options.url+"?user_token="+token
  },
  onShow () {
    let token = wx.getStorageSync("user_token");
    let province_info = wx.getStorageSync("province_info") ? JSON.parse(wx.getStorageSync("province_info")) : {};
    if (token) {
      // 已登录
      this.islogin = true;
      this.h5_url = `${common.sign_in_url}?user_token=${token}&province_id=${province_info.province_id || 0}`;
    } else {
      // 未登录
      wx.navigateTo({ url: '/pages/package_main/login/main' });
    }
    //  this.h5_url = options.url+"?user_token="+token
  }
};
</script>

<style lang="less" scoped>
.container {
  .mask {
    width: 180px;
    height: 87px;
    background: rgba(0, 0, 0, 1);
    opacity: 0.6;
    line-height: 87px;
    text-align: center;
    font-size: 14px;
    font-family: "PingFangSC-Regular";
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 6pt;
    .btn {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      opacity: 0;
      z-index: 10;
    }
  }
}
</style>


